﻿/*
 * This file is part of quickReminder, a software for gamers
 *
 * Copyright (C) 2006-2009 Lucas Romero
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml.Serialization;
using ICSharpCode.SharpZipLib.Zip;

namespace LucasCode.PublicTypes
{
    public class UploadException : Exception
    {
        public UploadException(string msg) : base(msg) { }
    };

    [Serializable]
    public class MatchFile : IStaticDataSource
    {
        public class UploadSetting
        {
            public int JpegCompression = 80;
        };

        public static UploadSetting UploadSettings = new UploadSetting();

        #region Private Properties
        //private static bool m_DoubleCompressFiles;
        private static int m_SpeedMeasurementInterval = 500;

        private string m_Comment;
        private string m_Status;

        private DateTime m_LastChanged;
        private DateTime m_DateCreated;
        private bool m_ShouldBeUploaded = true;
        private DateTime m_DateArchived = DateTime.Now;
        private MatchArchive m_Parent;
        private string m_TargetName = string.Empty;
        private bool m_AddToArchive;
        private string m_Filename;
        private bool m_WasAutoRenamed;
        private bool m_IsArchived = false;
        private long m_Size;
        #endregion

        #region Constructors
        private MatchFile()
        {
            
        }

        public MatchFile(string filename)
            : this(filename, Path.GetFileNameWithoutExtension(filename))
        {
            //nothing
        }

        public MatchFile(string filename, string targetname)
        {
            try
            {
                m_Filename = filename;
                m_TargetName = targetname;
                
                Size = new FileInfo(filename).Length;
            }
            catch (Exception ex) { System.Diagnostics.Trace.TraceError("Can't get MatchFile size: " + ex.Message); }
        }
        #endregion

        #region Properties
        //[XmlIgnore]
        //public static bool DoubleCompressFiles
        //{
        //    get { return m_DoubleCompressFiles; }
        //    set { m_DoubleCompressFiles = value; }
        //}

        [XmlIgnore]
        public bool AddToArchive
        {
            get { return m_AddToArchive; }
            set { m_AddToArchive = value; }
        }

        [XmlIgnore]
        public MatchFileType FileType
        {
            get
            {
                switch (Path.GetExtension(m_Filename).ToLower())
                {
                    case ".dem":
                        return MatchFileType.Replay;
                    case ".jpg":
                    case ".bmp":
                        return MatchFileType.Screenshot;
                    case ".zip":
                        if (Regex.Match(Path.GetFileNameWithoutExtension(m_Filename), @"^\d+$").Success)
                            return MatchFileType.Aequitas;
                        return MatchFileType.CompressedContent;
                    default:
                        return MatchFileType.Unknown;
                }
            }
        }

        [XmlIgnore]
        public bool WasAutoRenamed
        {
            get { return m_WasAutoRenamed; }
            set { m_WasAutoRenamed = value; }
        }

        [XmlIgnore]
        public MatchArchive Parent
        {
            get { return m_Parent; }
            set { m_Parent = value; }
        }

        [XmlIgnore]
        public bool WasUploaded { get; set; }

        [XmlIgnore]
        public bool ShouldBeUploaded
        {
            get { return m_ShouldBeUploaded; }
            set { m_ShouldBeUploaded = value; }
        }

        [XmlIgnore]
        public bool WasUnzipped
        {
            get; set;
        }

        public static int SpeedMeasurementInterval
        {
            get { return m_SpeedMeasurementInterval; }
            set { m_SpeedMeasurementInterval = value; }
        }

        public bool IsArchived
        {
            get { return m_IsArchived; }
            set { m_IsArchived = value; }
        }

        public string TargetName
        {
            get { return m_TargetName; }
            set { m_TargetName = value; }
        }

        public string Comment
        {
            get { return m_Comment; }
            set { m_Comment = value; }
        }

        public DateTime LastChanged
        {
            get
            {
                if (IsArchived)
                    return m_LastChanged;
                return File.GetLastWriteTime(m_Filename);
            }
            set { m_LastChanged = value; }
        }

        public DateTime DateCreated
        {
            get { return m_DateCreated; }
            set { m_DateCreated = value; }
        }

        public DateTime DateArchived
        {
            get { return m_DateArchived; }
            set { m_DateArchived = value; }
        }

        public string Filename
        {
            get { return m_Filename; }
            set { m_Filename = value; }
        }

        public long Size
        {
            get { return m_Size; }
            set { m_Size = value; }
        }

        public string Status
        {
            get { return m_Status; }
            set { m_Status = value; }
        }

        public string OldFilename
        {
            get; set;
        }
        #endregion

        #region Methods
        public Stream GetSource()
        {
            if (!WasUnzipped)
                return new FileStream(m_Filename, FileMode.Open, FileAccess.Read, FileShare.Read);
            else
            {
                //?? zuerst die datei aus dem zipfile in einen temp ordner extrahieren, dann den stream darauf zurück geben

                return null;
            }
            //?? hier notwendig wenn vorher abgefragt wird?return m_Parent.GetInputStream(this);
        }
        #endregion
    }
}
